package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"hcy-go/global/structs/enum"
	"hcy-go/global/structs/lms"
	"hcy-go/lib/id"
	"hcy-go/lib/response"
)

type coursewareApi struct {
}

var CoursewareApi = coursewareApi{}

func (a coursewareApi) Upload(c *gin.Context) {
	f, err := c.FormFile("file")
	if err != nil {
		response.BadWithParamsError(c)
		return
	}
	filename := fmt.Sprintf("%d_%s", id.GetSnowId(), f.Filename)
	err = c.SaveUploadedFile(f, filename)
	if err != nil {
		response.BadWithCode(c, enum.ErrorMap.CoursewareFileUploadFail)
		return
	}
	response.Ok(c, filename)
}

func (a coursewareApi) Create(c *gin.Context) {
	var input lms.AddCourseware
	if err := c.ShouldBindJSON(&input); err != nil {
		response.BadWithParamsError(c)
		return
	}
	response.OK204(c)
	return
}
